package com.lxl.bt.retorfit;

import android.os.Build;

import com.lxl.bt.BuildConfig;
import com.lxl.bt.Constant;
import com.lxl.bt.util.Prefs;

import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Author : xinlong.lv
 */
public class RetrofitUtil {

    private static final long TIME_OUT = 60 * 20;

//    private static final String HOST = "http://47.103.176.82/";
//    private static final String HOST = "http://122.114.94.174:8090/";
//    private static final String HOSTMain = "http://122.114.94.174:8091/";
    private static final String HOST = "http://api.puxkj.com/api/";

//    private static final String HOST = "http://47.100.191.42/mock/21/";

    private volatile static RetrofitUtil instance = null;

    private RetrofitUtil() {
        //防止有人利用反射恶意修改
        if (null != instance) {
            throw new RuntimeException("Don't construct more!");
        }
    }

    public static RetrofitUtil getInstance() {
        // 两层判空，第一层是为了避免不必要的同步
        // 第二层是为了在null的情况下创建实例
        if (instance == null) {
            synchronized (RetrofitUtil.class) {
                if (instance == null) {
                    instance = new RetrofitUtil();
                }
            }
        }
        return instance;
    }

    public static String getHOST() {
        return HOST;
    }
    //    public static RetrofitUtil baseUrl(String baseUrl) {
//        return new RetrofitUtil(baseUrl);
//    }


//    public synchronized static <T> T create(Class<T> clazz) {
//        return retrofitUtil.createFrom(clazz);
//    }

    public synchronized <T> T create(Class<T> clazz) {
        return getRetrofit().create(clazz);
    }
    public synchronized <T> T create(Class<T> clazz,int type) {
        return getRetrofit(type).create(clazz);
    }


    /**
     * 请求头
     */
    private static Headers addHeaders() {
        Headers.Builder builder = new Headers.Builder();
        builder.add("Content-Type", "application/json");
//        if(!TextUtils.isEmpty(App.getInstance().JSESSIONID))
//            builder.add("Cookie", App.getInstance().JSESSIONID);
        String modelVersionInfo = String.format("%s %s; Android %s; %s",
                Build.MANUFACTURER, Build.MODEL, Build.VERSION.RELEASE, BuildConfig.VERSION_NAME);
        builder.add("User-Agent", modelVersionInfo);
        builder.add("Authorization",Prefs.getStringValue(Constant.JSESSIONID));
        return builder.build();
    }
    private static Headers addHeaders(int type) {
        Headers.Builder builder = new Headers.Builder();
        switch (type) {
            case 0:
                builder.add("Content-Type", "application/json");
                break;
            case 1:
                builder.add("Content-Type", "application/x-www-form-urlencoded");
                break;
        }
//        if(!TextUtils.isEmpty(App.getInstance().JSESSIONID))
//            builder.add("Cookie", App.getInstance().JSESSIONID);

        String modelVersionInfo = String.format("%s %s; Android %s; %s",
                Build.MANUFACTURER, Build.MODEL, Build.VERSION.RELEASE, BuildConfig.VERSION_NAME);
        builder.add("User-Agent", modelVersionInfo);
        builder.add("Authorization",Prefs.getStringValue(Constant.JSESSIONID));
        return builder.build();
    }
    private Retrofit getRetrofit() {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
//        OkHttpClient.Builder client = getOkHttpClientBuilder();

        if (BuildConfig.DEBUG) {
            client.addInterceptor(new InterceptorLog());
        }

        client.addInterceptor(new InterceptorHeader(addHeaders()));
        client.cookieJar(CookiesPersistent.getInstance());

        client.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        client.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
        client.readTimeout(TIME_OUT, TimeUnit.SECONDS);

        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(HOST);
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.client(client.build());

        return builder.build();
    }
    private Retrofit getRetrofit(int type) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
//        OkHttpClient.Builder client = getOkHttpClientBuilder();

        if (BuildConfig.DEBUG) {
            client.addInterceptor(new InterceptorLog());
        }

        client.addInterceptor(new InterceptorHeader(addHeaders(type)));
        client.cookieJar(CookiesPersistent.getInstance());

        client.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        client.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
        client.readTimeout(TIME_OUT, TimeUnit.SECONDS);

        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(HOST);
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.client(client.build());

        return builder.build();
    }

    private OkHttpClient.Builder getOkHttpClientBuilder() {
        HttpsVerification.SSLParams sslParams = HttpsVerification.getSslSocketFactory();
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
//                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
//                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);

//        builder.addInterceptor(new RequestHeadIntercept());
//        builder.addInterceptor(new TokenIntercept());
//
//        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//        //BODY打印信息,NONE不打印信息
//        logging.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
//        builder.addInterceptor(logging);

        return builder;
    }


//    /**
//     * 创建API 实例
//     */
//    public synchronized static <T> T api(Class<T> clazz) {
//        OkHttpClient.Builder client = new OkHttpClient.Builder();
////        OkHttpClient.Builder client = getOkHttpClientBuilder();
//
////        client.addInterceptor(new InterceptorHeader(getHeaders()));
//        Retrofit.Builder builder = new Retrofit.Builder();
//        if (BuildConfig.DEBUG) {
//            client.addInterceptor(new InterceptorLog());
//            builder.baseUrl(HOST_TEST);
//        } else {
//            builder.baseUrl(HOST_RELEASE);
//        }
////        builder.addConverterFactory(G)
////        builder.addConverterFactory(GsonConverterFactory.create());
////        builder.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
//
//        builder.client(client.build());
//
//
//        return builder.build().create(clazz);
//    }
//
//    public static synchronized Retrofit getInstance(String host, Headers headers, boolean needLog) {
//        return new RetrofitUtil().getRetrofit(host, headers, needLog);
//    }
//
//    /**
//     * 创建Retrofit
//     */
//    private Retrofit getRetrofit(String host, Headers headers, boolean needLog) {
//        Retrofit.Builder builder = new Retrofit.Builder();
//        builder.baseUrl(host);
////        builder.addConverterFactory(GsonConverterFactory.create());
//        builder.client(getClient(headers, needLog));
//        return builder.build();
//    }
//
//    /**
//     * OkHttpClient
//     */
//    private OkHttpClient getClient(Headers headers, boolean needLog) {
//        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//
////        builder.cookieJar(new CookiesPersistent());
//
//        //是否添加请求头
//        if (headers != null) {
//            builder.addInterceptor(new InterceptorHeader(headers));
//        }
//
//        //是否打印log
//        if (needLog) {
//            builder.addInterceptor(new InterceptorLog());
//        }
//
//        return builder.build();
//    }

}
